//
//  MyPictureDetailsViewController.m
//  QT
//
//  Created by 甘世清 on 2024/10/24.
//

#import "MyPictureDetailsViewController.h"
#import "MyPictureTopView.h"
#import "PictureTableViewCell.h"
@interface MyPictureDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) MyPictureTopView * topView;
@end

@implementation MyPictureDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"详情");
    WS(weakSelf);
    [self zx_setRightBtnWithImg:SQGetImage(@"gengduo") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        [weakSelf SQ_reportButtonClick];
    }];
}

- (void)SQ_UIViewForSelfView{
    
 [self.topView.pictureImageView sd_setImageWithURL:SQURLImage(self.model.imgs) placeholderImage:SQGetImage(@"1")];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableHeaderView = self.topView;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self SQ_tableViewAddRefresh];
    [self SQ_Header];
}
-(void)SQ_reportButtonClick{
    
    WS(weakSelf);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"是否删除交换") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"删除") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"删除成功")];
        [weakSelf SQ_deleted];
    }];
   
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
}
-(void)SQ_deleted{
    WS(weakSelf);
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.model.userid forKey:@"id"];
    [HttpTool post:@"dynamic/delete" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"删除成功")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf SQ_popViewVC];
        });
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Header{
    WS(weakSelf);
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.model.userid forKey:@"id"];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:@"dynamic/exchangeList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.tableView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PictureModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
        [self.tableView.mj_header endRefreshing];
    }];
}
-(void)SQ_Foother{
    WS(weakSelf);
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.model.userid forKey:@"id"];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:@"dynamic/exchangeList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
       
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PictureModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
        [self.tableView.mj_footer endRefreshing];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PictureTableViewCell"];
    if (!cell) {
        cell = [[PictureTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"PictureTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [PictureModel new];
    [cell.visualEffectView removeFromSuperview];
    cell.messageLabel.hidden = YES;
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  240;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 50)];
    view.backgroundColor = UIColor.whiteColor;
    UILabel * la = [[UILabel alloc]init];
    la.text = SQNSLocalString(@"交换列表");
    la.textColor = UIColor.blackColor;
    la.font = FONT_MEDIUM(16);
    [view addSubview:la];
    [la mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).offset(20);
            make.centerY.equalTo(view);
    }];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return FootHeight+20;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]init];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offSetH = scrollView.contentOffset.y;
    if (offSetH > 200) {
        self.zx_navBarBackgroundColor = UIColor.whiteColor;
    } else {
        self.zx_navBarBackgroundColor = UIColor.clearColor;
    }
}
- (MyPictureTopView *)topView{
    if (!_topView) {
        _topView = [[MyPictureTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 300)];
    }
    return _topView;
}

@end
